package aop.annotation;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.stereotype.Component;

/**
 * @date 2020/5/3 12:45
 */
@Component
//切面
@Aspect
public class MyAspectCGLIB {
	//定义切入点 ,语法：execution(修饰符  返回值  包.类.方法名(参数) throws异常)
	@Pointcut("execution(* aop.annotation.Bike.*(..))")
	public void aspectBike(){

	}

	@Before("aspectBike()")
	public void testCarBefore(){
		System.out.println("1、bike before---");
	}


	@After("aspectBike()")
	public void testCarAfter(){
		System.out.println("3、bike after---");
	}


	/**
	 * 环绕通知的功能等价于前置+执行+后置。重点掌握环绕通知即可
	 */
	@Around("aspectBike()")
	public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println(" 0 环绕通知-前-开启事务...，等效于before");
		//放行，pjp.proceed方法前后嵌入了@Before和@After
		/**
         * 调用{@link MethodInvocationProceedingJoinPoint#proceed()} 方法，
		 * 内部调用{@link ReflectiveMethodInvocation#proceed()} 方法，逻辑是递归调用before、after
		 */
		Object returnValue = pjp.proceed();
		System.out.println(" 4 环绕通知-后-关闭事物...，等效于after");
		return returnValue;
	}

//	@AfterReturning("aspectBike()")
//	public void testCarAfterReturning(){
//		System.out.println("2、bike invoke returning---");
//	}
//
//
//	@AfterThrowing("aspectBike()")
//	public void testCarAfterThrowing(){
//		System.out.println("bike invoke throw exception---");
//	}

}

